﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.XPath;

public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        //EveParser parser = new EveParser("3255146", "cCWTbY6VOazDuZIzRCRfFRo9UMYKgs6dD0MY9BHBezmc2ICG2BvAW00ZU6bGMQ6H");
        
        EveAccount account = new EveAccount("3255146", "cCWTbY6VOazDuZIzRCRfFRo9UMYKgs6dD0MY9BHBezmc2ICG2BvAW00ZU6bGMQ6H");
        ddCharacterList.DataSource = account.CharacterList;
        ddCharacterList.DataTextField = "Name";
        ddCharacterList.DataValueField = "CharacterID";
        ddCharacterList.DataBind();

        //EveAPIParser.GetXmlDocument(EveAPI.CharacterList, EveAPIParser.GeneratePostData(account.UserID, account.ApiKey,account.CharacterList[0].CharacterID)).Save(Server.MapPath("CharacterList.xml"));

        /*foreach (EveCharacter character in account.CharacterList)
        {
            litDisplay.Text += character.Name + "<br />";
            litDisplay.Text += character.CharacterID + "<br />";
            litDisplay.Text += character.CorporationID + "<br />";
            litDisplay.Text += character.CorporationName + "<br />";
        }*/
        //litDisplay.Text += "<hr>"+parser.GetXmlDocument(EveAPI.CharacterSheet).InnerXml.Replace("<","&lt;");
        //litDisplay.Text += "<hr>"+parser.GetXmlDocument(EveAPI.SkillsInTraining).InnerXml.Replace("<","&lt;");
     

        //XmlDocument xdoc = new XmlDocument();
        //litDisplay.Text = POSTTest().Replace("<","&lt;").Replace(">","&gt;");
        /*XmlDocument xdoc = POSTTest();
        XPathNavigator xnav = xdoc.CreateNavigator();
        XPathExpression xexpr;
        xexpr = xnav.Compile("/eveapi/result/rowset/row");
        XPathNodeIterator iterator = xnav.Select(xexpr);
        while (iterator.MoveNext())
        {
            XPathNavigator xnav2 = iterator.Current.Clone();
            litDisplay.Text += xnav2.GetAttribute("name","") + "<br />";
        }*/
        //litDisplay.Text = xdoc.GetElementsByTagName("row")[0].InnerText;
        //litDisplay.Text = xdoc.InnerXml.Replace("<","&lt;").Replace(">","&gt;");
        //litDisplay.Text += ParseCharacters(POSTTest());
        /*string user_id = "3255146";
        string apikey = "cCWTbY6VOazDuZIzRCRfFRo9UMYKgs6dD0MY9BHBezmc2ICG2BvAW00ZU6bGMQ6H";
        // we have to include the user id and the api key in every request
        string postdata = String.Format("userid={0}&apikey={1}", Uri.EscapeDataString(user_id), Uri.EscapeDataString(apikey));
        //string postData = "userid="+Uri.EscapeDataString(userid)+"&apikey="+Uri.EscapeDataString(apikey);
        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes(postdata);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.eve-online.com/account/Characters.xml.aspx");
        request.Method = "POST";
        request.ContentType = "application/x-www-urlencoded";
        request.ContentLength = postdata.Length;
        Stream myStream = request.GetRequestStream();
        myStream.Write(data, 0, data.Length);
        myStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            Response.Write(reader.ReadToEnd());
        }*/
    }

    private String readHtmlPage(string url)
    {
        String result = "";
        String strPost = "x=1&y=2&z=YouPostedOk";
        StreamWriter myWriter = null;
        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
        objRequest.Method = "POST";
        objRequest.ContentLength = strPost.Length;
        objRequest.ContentType = "application/x-www-form-urlencoded";
        try
        {
            myWriter = new StreamWriter(objRequest.GetRequestStream());
            myWriter.Write(strPost);
        }
        catch (Exception e)
        {
            return e.Message;
        }
        finally
        {
            myWriter.Close();
        }

        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
        using (StreamReader sr =
           new StreamReader(objResponse.GetResponseStream()))
        {
            result = sr.ReadToEnd();

            // Close and clean up the StreamReader
            sr.Close();
        }
        return result;
    }

    private XmlDocument POSTTest()
    {
        string retval;
        XmlDocument xdocretval = new XmlDocument();
        string userid = "3255146";
        string apikey = "cCWTbY6VOazDuZIzRCRfFRo9UMYKgs6dD0MY9BHBezmc2ICG2BvAW00ZU6bGMQ6H";

        //HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create("http://api.eve-online.com/char/SkillInTraining.xml.aspx");
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://api.eve-online.com/account/Characters.xml.aspx");
        string postData = "userid="+Uri.EscapeDataString(userid)+"&apikey="+Uri.EscapeDataString(apikey);
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] byte1 = encoding.GetBytes(postData);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
        request.ContentLength = postData.Length;
        Stream stream = request.GetRequestStream();
        stream.Write(byte1, 0, byte1.Length);
        stream.Close();
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            xdocretval.Load(reader);
        }
        return xdocretval;
    }

    private string ParseCharacters(XmlDocument xmldoc)
    {
        StringBuilder sb = new StringBuilder();
        XmlNodeList characters = xmldoc.GetElementsByTagName("characters");
        for (int i = 0; i < characters.Count; i++)
        {
            sb.Append(characters[i].InnerText);
        }
        return sb.ToString();
    }
}
